<?php
/**
 * country.php
 *
 * Created on 2014-12-8
 * Create by LDD
 */
namespace haibao\cms\data;

use \haibao\cms\model\filter\Event as EventFilter;
use \haibao\cms\model\filter\EventKeywords as EventKeywordsFilter;
use \haibao\cms\model\filter\EventTag as EventTagFilter;
use \haibao\frame\data\query\Condition as Condition;

class Event extends \haibao\cms\data\BaseMysql{
    
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\EventDefine');
	}

	/**
	 * @param \haibao\cms\model\data\EventDefine $eventModel
	 */
	public function add($eventModel){
	    
		$this->beginTranscation();
		try{
			parent::add($eventModel);
			if($eventModel->TagIds){
				$this->addEventTag($eventModel);
			}
			if($eventModel->Keywords){
				$this->addEventKeywords($eventModel);
			}
			if($eventModel->Categories){
				$this->addEventCategory($eventModel);
			}
		}catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	/**
	 * @param \haibao\cms\model\data\EventDefine $eventModel
	 */
	
	public function addEventKeywords($eventModel){
		$arrValues = array();
		$arrSql = array();
		if($eventModel->Keywords){
			$sql = "insert into EventKeywords(EventId,KeywordId) values";
			foreach($eventModel->Keywords as $keyword){
				array_push($arrSql,'(%s,%s)');
				array_push($arrValues,$eventModel->Id);
				array_push($arrValues,$keyword->KeywordId);
			}
			$sql .= implode(',', $arrSql);
			$this->query($sql,$arrValues);
		}
	}
	/**
	 * @param array $eventIds
	 */
	public function getEventNameByIds($eventIds){
		$eventFilter = new EventFilter();
		$eventFilter->select(array(
				EventFilter::CONDITION_FIELD_NAME_EVENT_NAME,
				EventFilter::CONDITION_FIELD_NAME_ID
		));
		$eventFilter->where($eventFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_IN, $eventIds);
		$eventFilter->where($eventFilter::CONDITION_FIELD_NAME_STATUS, \haibao\frame\data\query\Condition::CONDITION_EQUAL, \haibao\cms\model\data\EventDefine::STATUS_ENABLE);
		
		$events =  $this->getAll($eventFilter);
		$eventNames = array();
		if($events){
		 	foreach($events as $key=>$event){
		 		$eventNames[$event->Id] = $event->EventName;
		 	}	
		}
		return $eventNames;
	}
	/**
	 * @param int $eventId
	 */
	public function getEventById($eventId){
		$eventFilter = new EventFilter();
		$eventTagFilter = new EventTagFilter();
		$eventKeywordFilter = new EventKeywordsFilter();
		
		$eventFilter->select(array(
				EventFilter::CONDITION_FIELD_NAME_ADDRESS,
				EventFilter::CONDITION_FIELD_NAME_CITY_VALUE,
				EventFilter::CONDITION_FIELD_NAME_COUNTRY_VALUE,
				EventFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				EventFilter::CONDITION_FIELD_NAME_END_DATE,
				EventFilter::CONDITION_FIELD_NAME_END_TIME,
				EventFilter::CONDITION_FIELD_NAME_EVENT_NAME,
				EventFilter::CONDITION_FIELD_NAME_ID,
				EventFilter::CONDITION_FIELD_NAME_START_DATE,
				EventFilter::CONDITION_FIELD_NAME_START_TIME,
				EventFilter::CONDITION_FIELD_NAME_STATUS,
				EventFilter::CONDITION_FIELD_NAME_ALIAS		
		));
		$eventTagFilter->select(array(
				EventTagFilter::CONDITION_FIELD_NAME_ID,
				EventTagFilter::CONDITION_FIELD_NAME_TAG_ID,
				EventTagFilter::CONDITION_FIELD_NAME_EVENT_ID
		));
		$eventKeywordFilter->select(array(
				EventKeywordsFilter::CONDITION_FIELD_NAME_ID,
				EventKeywordsFilter::CONDITION_FIELD_NAME_EVENT_ID,
				EventKeywordsFilter::CONDITION_FIELD_NAME_KEYWORD_ID
		));
		$fromkeyConditionKey = array(
				new Condition(EventFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, EventKeywordsFilter::CONDITION_FIELD_NAME_EVENT_ID,$eventFilter,$eventKeywordFilter)
		);	
		$eventFilter->leftJoin($eventKeywordFilter,null,$fromkeyConditionKey,null,$eventFilter::CONDITION_FIELD_NAME_KEYWORDS);
		
		$fromTagConditionKey = array(
				new Condition(EventFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, EventTagFilter::CONDITION_FIELD_NAME_EVENT_ID,$eventFilter,$eventTagFilter),
		);	
		$eventFilter->leftJoin($eventTagFilter,null,$fromTagConditionKey,null,EventFilter::CONDITION_FIELD_NAME_TAGS);
		
		$eventFilter->where($eventFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_EQUAL, $eventId);
		$eventFilter->where($eventFilter::CONDITION_FIELD_NAME_STATUS, \haibao\frame\data\query\Condition::CONDITION_EQUAL, \haibao\cms\model\data\EventDefine::STATUS_ENABLE);
		
		return parent::getOne($eventFilter);
	}
	/**
	 * @param \haibao\cms\model\data\EventDefine $eventModel
	 */
	public function save($eventModel){
		$this->beginTranscation();
		try{
			parent::updateById($eventModel->Id, array(
					EventFilter::CONDITION_FIELD_NAME_ADDRESS=>$eventModel->Address,
					EventFilter::CONDITION_FIELD_NAME_CITY_VALUE=>$eventModel->CityValue,
					EventFilter::CONDITION_FIELD_NAME_COUNTRY_VALUE=>$eventModel->CountryValue,
					EventFilter::CONDITION_FIELD_NAME_DESCRIPTION =>$eventModel->Description,
					EventFilter::CONDITION_FIELD_NAME_END_DATE =>$eventModel->EndDate,
					EventFilter::CONDITION_FIELD_NAME_END_TIME=>$eventModel->EndTime,
					EventFilter::CONDITION_FIELD_NAME_EVENT_NAME =>$eventModel->EventName,
					EventFilter::CONDITION_FIELD_NAME_START_DATE=>$eventModel->StartDate,
					EventFilter::CONDITION_FIELD_NAME_START_TIME =>$eventModel->StartTime,
					EventFilter::CONDITION_FIELD_NAME_ALIAS =>$eventModel->Alias
					));
			
			$tagSql ="delete from EventTag where EventId=%s";
			$keySql = "delete from EventKeywords where EventId=%s";
			$this->query($tagSql,array($eventModel->Id));
			$this->query($keySql,array($eventModel->Id));
			$this->addEventTag($eventModel);
			$this->addEventKeywords($eventModel);
			$this->addEventCategory($eventModel);
		}catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	/**
	 * @param \haibao\cms\model\data\EventDefine $eventModel
	 */
	public function addEventTag($eventModel){
		$arrValues = array();
		$arrSql = array();
		if($eventModel->Tags){
			$sql = "insert into EventTag(EventId,TagId) values";
			foreach($eventModel->Tags as $Tag){
				array_push($arrSql,'(%s,%s)');
				array_push($arrValues,$eventModel->Id);
				array_push($arrValues,$Tag->TagId);
			}
			$sql .= implode(',', $arrSql);
			$this->query($sql,$arrValues);
		}
	}
	
	public function delete($eventId,$arrSet){
		$this->beginTranscation();
		try{
			parent::updateById($eventId,$arrSet);
			
			$tagSql ="delete from EventTag where EventId=%s";
			$keySql = "delete from EventKeywords where EventId=%s";
			$cateSql ="delete from EventCategory where EventId=%s";
			$this->query($tagSql,array($eventId));
			$this->query($keySql,array($eventId));
			$this->query($cateSql,array($eventId));
		}catch (\Exception $e) {
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	/**
	 * 事件搜索
	 * @param \haibao\cms\model\filter\Event $eventFilter
	 */
	public function getAll($eventFilter){
		return parent::getAll($eventFilter);
	}
	public function getOne($eventFilter){
		return parent::getOne($eventFilter);
	}
	public function getKeywordByName($keyword){
		$data = array();
		
		$sql ="select * from EventKeywords where Keywords like %s";
		$result = $this->query($sql,array('%'.$keyword."%"));
		
		while($result->fetch_assoc()){
			$eventKeywordId = $result->getData('Id');
			$data[$eventKeywordId] = $result->getData('Keywords');
		}
		return $data;
	}
	
	public function addEventCategory($eventModel){
		$item = array();
		$param = array();
		$sql="delete from EventCategory where EventId = %s";
		$this->query($sql,array($eventModel->Id));
		if($eventModel->Categories){
			$sql="insert into EventCategory (EventId,CategoryId) values ";
			foreach($eventModel->Categories as $key => $value){
				array_push($item, '(%s,%s)');
				array_push($param, $eventModel->Id);
				array_push($param, $value->CategoryId);
			}
			$sql .= implode(',', $item);
			$this->query($sql,$param);
		}
		
	}
}